package com.bootdo.common;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.springframework.stereotype.Component;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

public class SimpleFixedThreadPoolUtil {
	
	/** 默认线程个数 */
    private static final int DEFAULT_THREAD = Runtime.getRuntime().availableProcessors();//返回到Java虚拟机的可用的处理器数量
    /** 线程池个数: 默认为： Runtime.getRuntime().availableProcessors() * 2 */
    public static int THREADPOOL_NUM = DEFAULT_THREAD * 2;
    private static ExecutorService executor = Executors.newFixedThreadPool(THREADPOOL_NUM,
            new ThreadFactoryBuilder().setNameFormat("SimpleFixedThreadPool- %d: ").build());

    /**
     * 提交任务,任务执行后无返回值
     * 
     * @param task
     */
    public static void addTask(Runnable task) {
        executor.execute(task);
    }

    /**
     * 提交具有返回值的任务
     * @param task
     * @return
     */
    public static <T> Future<T> addTask(Callable<T> task) {
        Future<T> future = executor.submit(task);
        return future;
    }

    /**
     * 关闭线程池
     */
    /*public void shutdown() {
        executor.shutdown();
    }*/

}
